package com.phantasy.scene.sceneUnit.baseUnit.things.avatar
{
	import com.phantasy.globals.GameConfig;
	import com.phantasy.proto.p_skin;
	import com.phantasy.scene.sceneUnit.baseUnit.OnlyIDCreater;
	import com.phantasy.scene.sceneUnit.baseUnit.things.heartbeat.ThingFrameFrequency;
	import com.phantasy.scene.sceneUnit.baseUnit.things.resource.SourceManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.DataEvent;

	/**
	 *化身 
	 * @author Liseen
	 * 
	 */	
	public class Avatar extends Sprite
	{
		
		public static const BODY_COMPLETE:String = "BodyComplete";
		
		private var onlyKey:String;//唯一标识
		private var _selectStateAndDir:String;
		private var _shadow:Bitmap;
		private var _body:Part;
		private var _downMounts:Part;
		private var _wind:Part;
		private var _weapon:Part;
		private var _weaponEffect:Part;
		private var _upMounts:Part;
		private var _selectState:String = "stand";
		private var _skinData:p_skin;
		private var bodyURL:String;
		private var weaponURL:String;
		private var weaponEffectURL:String = "";
		private var windURL:String = "";
		private var mountsURL:String = "";
		private var bodyStateURL:String = "";
		private var bodyComplete:Boolean = false;
		private var weaponStateURL:String = "";
		private var weaponEffectStateURL:String = "";
		private var downMountsStateURL:String = "";
		private var upMountsStateURL:String = "";
		private var windStateURL:String = "";
		private var _preState:String = "";
		private var _preStateHeight:int = 0;
		
		public var category:int;//类别
		public var sex:int;//性别
		public var _effectLayerTop:Sprite;
		public var _effectLayerBottom:Sprite;
		public var _bodyLayer:Sprite;
		public var isStop:Boolean = false;
		public var selectDir:int = 4;
		public var selectSpeed:int = 4;
		public function Avatar()
		{
			this.onlyKey = OnlyIDCreater.createID();
			this._selectStateAndDir = AvatarConstant.ACTION_STAND.concat(AvatarConstant.DIR_DOWN);
		}
		
		public function getBodyURL():String
		{
			return this.bodyStateURL;
		}
		
		public function get skinData():p_skin
		{
			return this._skinData;
		}
		
		public function get selectState():String
		{
			return this._selectState;
		}
		
		public function initSkin(skinData:p_skin, category:int, sex:int=0):void
		{
			this.category = category;
			this.sex = sex;
			SourceManager.getInstance().addEventListener(SourceManager.AVATAR_CREATE_COMPLETE, this.sourceCreateComplete);
			this.createLayer();
			this.createPosition(skinData.skinid);
			this.createSkinURLS(skinData,category);
			this.checkURL();
		}
		
		public function createLayer():void
		{
			this.addShadow();
			this._effectLayerBottom = new Sprite();
			this._effectLayerBottom.mouseEnabled = false;
			this._effectLayerBottom.mouseChildren = false;
			addChild(this._effectLayerBottom);
			this._bodyLayer = new Sprite();
			this._bodyLayer.mouseEnabled = false;
			this._bodyLayer.mouseChildren = false;
			addChild(this._bodyLayer);
			this._effectLayerTop = new Sprite();
			this._effectLayerTop.mouseEnabled = false;
			this._effectLayerTop.mouseChildren = false;
			addChild(this._effectLayerTop);
		}
		
		public function createPosition(skinID:int):void
		{
			if(skinID<AvatarConstant.MAN_MONSTER_ID){
				
				this._downMounts = new Part();
				this._bodyLayer.addChild(this._downMounts);
				this._wind = new Part();
				this._bodyLayer.addChild(this._wind);
				this._body = new Part();
				this._body.endFunction = this.onActionEnd;
				this._bodyLayer.addChild(this._body);
				this._weapon = new Part();
				this._bodyLayer.addChild(this._weapon);
				this._weaponEffect = new Part();
				this._bodyLayer.addChild(this._weaponEffect);
				this._upMounts = new Part();
				this._bodyLayer.addChild(this._upMounts);
				
			}else{
				
				this._body = new Part();
				this._body.endFunction = this.onActionEnd;
				this._bodyLayer.addChild(this._body);
				
			}
		}
		
		public function stop():void
		{
			this.isStop = true;
			if (this._body)
			{
				this._body.stop();
			}
			if (this._weapon)
			{
				this._weapon.stop();
			}
			if (this._weaponEffect)
			{
				this._weaponEffect.stop();
			}
			if (this._downMounts)
			{
				this._downMounts.stop();
			}
			if (this._upMounts)
			{
				this._upMounts.stop();
			}
			if (this._wind)
			{
				this._wind.stop();
			}
		}
		
		public function play(_arg1:String, _arg2:int, _arg3:int, _arg4:Boolean=false):void
		{
		
		}
		
		/**
		 *更新皮肤 
		 * @param pVo
		 * 
		 */		
		public function updataSkin(pVo:p_skin):void
		{
			
		}
		
		/**
		 *创建影子 
		 * 
		 */		
		private function addShadow():void
		{
			if(!this._shadow){
				_shadow = new Bitmap(SourceManager.getInstance().getShadow());
				this._shadow.x = -27;
				this._shadow.y = -14;
				addChild(this._shadow);
			}
		}
		
		private function onActionEnd():void
		{
			switch (this.selectState)
			{
				case AvatarConstant.ACTION_SIT:
					this.stop();
					return;
				case AvatarConstant.ACTION_DIE:
					this.stop();
					return;
				default:
					this.play(AvatarConstant.ACTION_STAND, this.selectDir, ThingFrameFrequency.STAND, true);
			}
		}
		
		private function createSkinURLS(pvo:p_skin,category:int):void
		{
			var _local3:int;
			var _local4:int;
			this._skinData = pvo;
			if(pvo.skinid<AvatarConstant.MAN_MONSTER_ID){
				
				_local3 = pvo.skinid % 2;
				_local4 = pvo.clothes;
				
				if ((_local4 == 31010601) || (_local4 == 31300601)|| (_local4 == 32010601) || (_local4 == 32300601)|| (_local4 == 33010601) || (_local4 == 33300601))
				{
					this.bodyURL = (((((GameConfig.DEFULT_AVATAR + "human/") + category) + "/0/") + _local3) + "/");
				}
				else
				{
					this.bodyURL = (((((((GameConfig.DEFULT_AVATAR + "human/") + category) + "/") + _local4) + "/") + _local3) + "/");
				};
				if (pvo.weapon == 0)
				{
					this.weaponURL = "";
				}
				else
				{
					this.weaponURL = (((((GameConfig.DEFULT_AVATAR + "weapon/") + pvo.weapon) + "/") + _local3) + "/");
					this.weaponEffectURL = (((((GameConfig.DEFULT_AVATAR + "weaponEffect/") + this.category) + "/") + this.sex) + "/");
				}
				if (pvo.wind == 0)
				{
					this.windURL = "";
				}
				else
				{
					this.windURL = (((GameConfig.DEFULT_AVATAR + "wind/") + pvo.wind) + "/");
				}
				if (pvo.mounts == 0)
				{
					this.mountsURL = "";
				}
				else
				{
					this.bodyURL = (this.bodyURL + "mount/");
					if (this.weaponURL != "")
					{
						this.weaponURL = (this.weaponURL + "mount/");
					}
					if (this.windURL != "")
					{
						this.windURL = (this.windURL + "mount/");
					}
					if (this.weaponEffectURL != "")
					{
						this.weaponEffectURL = (this.weaponEffectURL + "mount/");
					}
					this.mountsURL = (((GameConfig.DEFULT_AVATAR + "mount/") + pvo.mounts) + "/");
				}
			}else{
				
				this.bodyURL = GameConfig.DEFULT_AVATAR + "monster/" + pvo.skinid + "/";
				
			}
			
		}
		
		private function checkURL():void
		{
			if (this.bodyURL != "")
			{
				if ((((this._selectState == AvatarConstant.ACTION_STAND)) || ((this._selectState == AvatarConstant.ACTION_WALK))))
				{
					this.bodyStateURL = (this.bodyURL + "runAndStand.swf");
				}
				else
				{
					this.bodyStateURL = ((this.bodyURL + this._selectState) + ".swf");
				}
				if (!(((this.skinData.skinid >= AvatarConstant.MAN_MONSTER_ID)) && ((this._selectState == AvatarConstant.ACTION_DIE))))
				{
					this.checkUrlIsExist(this.bodyStateURL, true);
				}
			}
			else
			{
				this.bodyStateURL = "";
			}
			if (this.weaponURL != "")
			{
				if ((((this._selectState == AvatarConstant.ACTION_STAND)) || ((this._selectState == AvatarConstant.ACTION_WALK))))
				{
					this.weaponStateURL = (this.weaponURL + "runAndStand.swf");
				}
				else
				{
					this.weaponStateURL = ((this.weaponURL + this._selectState) + ".swf");
				}
				this.checkUrlIsExist(this.weaponStateURL);
				if ((((this._selectState == AvatarConstant.ACTION_ATTACK)) && (!((this.category == 3)))))
				{
					this.weaponEffectStateURL = (this.weaponEffectURL + "attack.swf");
					this.checkUrlIsExist(this.weaponEffectStateURL);
				}
				else
				{
					this.weaponEffectStateURL = "";
				}
			}
			else
			{
				this.weaponStateURL = (this.weaponEffectStateURL = "");
			}
			if (this.mountsURL != "")
			{
				if ((((this._selectState == AvatarConstant.ACTION_STAND)) || ((this._selectState == AvatarConstant.ACTION_WALK))))
				{
					this.downMountsStateURL = ((this.mountsURL + "down/") + "runAndStand.swf");
					this.upMountsStateURL = ((this.mountsURL + "up/") + "runAndStand.swf");
				}
				else
				{
					this.downMountsStateURL = (((this.mountsURL + "down/") + this._selectState) + ".swf");
					this.upMountsStateURL = (((this.mountsURL + "up/") + this._selectState) + ".swf");
				}
				if (((!((this._selectState == AvatarConstant.ACTION_SIT))) && (!((this._selectState == AvatarConstant.ACTION_DIE)))))
				{
					this.checkUrlIsExist(this.downMountsStateURL);
					this.checkUrlIsExist(this.upMountsStateURL);
				}
			}
			else
			{
				this.downMountsStateURL = (this.upMountsStateURL = "");
			}
			if (this.windURL != "")
			{
				if ((((this._selectState == AvatarConstant.ACTION_STAND)) || ((this._selectState == AvatarConstant.ACTION_WALK))))
				{
					this.windStateURL = (this.windURL + "runAndStand.swf");
				}
				else
				{
					if (this._selectState == AvatarConstant.ACTION_ATTACK)
					{
						if (this._skinData.mounts == 0)
						{
							this.windStateURL = ((((((this.windURL + this.category) + "/") + (this._skinData.skinid % 2)) + "/") + this._selectState) + ".swf");
						}
						else
						{
							this.windStateURL = ((this.windURL + this._selectState) + ".swf");
						};
					}
					else
					{
						this.windStateURL = ((this.windURL + this._selectState) + ".swf");
					}
				}
				this.checkUrlIsExist(this.windStateURL);
			}
			else
			{
				this.windStateURL = "";
			}
		}
		
		
		private function checkUrlIsExist(_arg1:String, _arg2:Boolean=false):void
		{
			if (SourceManager.getInstance().getResource(_arg1) == null)
			{
				if (_arg2 == true)
				{
					this.bodyComplete = false;
				}
				SourceManager.getInstance().load(_arg1, "avatar");
			}
			else
			{
				if (this.bodyComplete == false)
				{
					this.bodyComplete = true;
				}
			}
		}
		
		private function sourceCreateComplete(event:DataEvent):void
		{
			if ((((((((((event.data == this.bodyStateURL)) || ((event.data == this.weaponStateURL)))) || ((event.data == this.windStateURL)))) || ((event.data == this.upMountsStateURL)))) || ((event.data == this.downMountsStateURL))))
			{
				if (this.bodyComplete == false)
				{
					this.bodyComplete = true;
				}
				this.updataBMC();
			}
		}
		
		private function updataBMC():void
		{
			if ((((this.selectState == AvatarConstant.ACTION_STAND)) || ((this.selectState == AvatarConstant.ACTION_WALK))))
			{
				this.display(true);
			}
			else
			{
				this.display(false);
			}
		}
		
		protected function display(_arg1:Boolean=false):void
		{
			if (this.bodyComplete)
			{
				if (this.skinData.skinid < AvatarConstant.MAN_MONSTER_ID)
				{
					this._downMounts.play(this.downMountsStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
					if ((((((this._selectState == AvatarConstant.ACTION_ATTACK)) && ((this.category == 1)))) && ((this.sex == 1))))
					{
						if (this.weaponStateURL == "")
						{
							this._wind.play("", this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._body.play(this.bodyStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._weapon.play(this.windStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._upMounts.play(this.upMountsStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						}
						else
						{
							this._wind.play(this.bodyStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._body.play(this.weaponStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._weapon.play(this.windStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._upMounts.play(this.upMountsStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						};
						if (((!((this.weaponEffectStateURL == ""))) && ((this._selectState == AvatarConstant.ACTION_ATTACK))))
						{
							this._weaponEffect.play(this.weaponEffectStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						}
						else
						{
							this.stopPart(this._weaponEffect);
						};
						return;
					};
					if ((((((this.selectDir == AvatarConstant.DIR_UP)) || ((this.selectDir == AvatarConstant.DIR_RIGHT_UP)))) || ((this.selectDir == AvatarConstant.DIR_LEFT_UP))))
					{
						if (this.weaponStateURL == "")
						{
							this._wind.play("", this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._body.play(this.bodyStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._weapon.play(this.windStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._upMounts.play(this.upMountsStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						}
						else
						{
							this._wind.play(this.bodyStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._body.play(this.weaponStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._weapon.play(this.windStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							this._upMounts.play(this.upMountsStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						};
					}
					else
					{
						this._body.play(this.bodyStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						this._wind.play(this.windStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						this._upMounts.play(this.upMountsStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
						this._weapon.play(this.weaponStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
					};
					if (((!((this.weaponEffectStateURL == ""))) && ((this._selectState == AvatarConstant.ACTION_ATTACK))))
					{
						this._weaponEffect.play(this.weaponEffectStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
					}
					else
					{
						this.stopPart(this._weaponEffect);
					};
				}
				else
				{
					if ((((((((((((((this.skinData.skinid == 12004)) || ((this.skinData.skinid == 12008)))) || ((this.skinData.skinid == 11005)))) || ((this.skinData.skinid == 12010)))) || ((this.skinData.skinid == 12106)))) || ((this.skinData.skinid == 12205)))) || ((((this.skinData.skinid >= 12081)) && ((this.skinData.skinid <= 12087))))))
					{
						if (this._selectState == AvatarConstant.ACTION_STAND)
						{
							this._body.play(this.bodyStateURL, AvatarConstant.ACTION_STAND, this.selectDir, ThingFrameFrequency.FLY_STAND, _arg1);
						}
						else
						{
							if (this._selectState == AvatarConstant.ACTION_WALK)
							{
								this._body.play(this.bodyStateURL, AvatarConstant.ACTION_STAND, this.selectDir, ThingFrameFrequency.FLY, _arg1);
							}
							else
							{
								this._body.play(this.bodyStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
							}
						}
					}
					else
					{
						this._body.play(this.bodyStateURL, this._selectState, this.selectDir, this.selectSpeed, _arg1);
					}
				}
			}
			else
			{
				this.stopPart(this._body);
				this.stopPart(this._weapon);
				this.stopPart(this._weaponEffect);
				this.stopPart(this._downMounts);
				this.stopPart(this._upMounts);
				this.stopPart(this._wind);
				this._body.setTransparent();
			}
			this.checkStateChange();
		}
		
		private function checkStateChange():void
		{
			var _local1:int;
			var _local2:DataEvent;
			if (this._preState != this.selectState)
			{
				if (this.mountsURL != "")
				{
					_local1 = 150;
				}
				else
				{
					_local1 = this._bodyLayer.height;
				};
				if (_local1 != this._preStateHeight)
				{
					this._preStateHeight = _local1;
					_local2 = new DataEvent(BODY_COMPLETE);
					_local2.data = _local1.toString();
					dispatchEvent(_local2);
				}
			}
		}
		
		private function stopPart(_arg1:Part):void
		{
			if (_arg1 != null)
			{
				_arg1.stop();
				_arg1.clean();
			}
		}
		
	}
}